<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" size="mini">
      <el-form-item label="举报单号" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入举报单号" clearable size="small" />
      </el-form-item>
      <el-form-item label="举报状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable size="small">
          <el-option label="未处理" :value="0" />
          <el-option label="已处理" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="userReportList">
      <el-table-column label="举报单号" align="center" prop="code" />
      <el-table-column label="举报类型" align="center" prop="reportType">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.reportType===1">用户举报</el-tag>
          <el-tag v-else-if="scope.row.reportType===2">评论举报</el-tag>
          <el-tag v-else>--</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="举报人" align="center" prop="accountId" />
      <el-table-column label="举报时间" align="center" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="举报对象" align="center" prop="reportAccountId" />
      <el-table-column label="举报状态" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.status===0?'未处理':'已处理'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理结果" align="center" prop="handleStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.handleStatus===1">有效举报</el-tag>
          <el-tag v-else-if="scope.row.handleStatus===2">恶意举报</el-tag>
          <el-tag v-else-if="scope.row.handleStatus===3">无效举报</el-tag>
          <el-tag v-else>--</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)">查看</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户举报处理对话框 -->
    <el-dialog title="查看" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="举报单号" prop="code">
          {{form.code}}
        </el-form-item>
        <el-form-item label="举报类型">
          <el-tag v-if="form.reportType===1">用户举报</el-tag>
          <el-tag v-else-if="form.reportType===2">评论举报</el-tag>
        </el-form-item>
        <el-form-item label="举报人" prop="accountId">
          {{form.accountId}}
        </el-form-item>
        <el-form-item label="举报时间" prop="reportTime">
          {{ parseTime(form.reportTime) }}
        </el-form-item>
        <el-form-item label="举报对象" prop="reportAccountId">
          {{form.reportAccountId}}
        </el-form-item>
        <el-form-item label="举报内容" prop="reportText">
          {{form.reportText}}
        </el-form-item>
        <el-form-item label="证据截图" prop="reportImgs">
          <el-image v-for="(url, index) in form.reportImgs" :key="index" :src="url" fit="fill" :preview-src-list="[url]" />
        </el-form-item>
        <el-form-item label="举报原因" prop="reportReason">
          {{form.reportReason}}
        </el-form-item>
        <el-form-item label="举报来源" prop="reportSource">
          {{form.reportSource}}
        </el-form-item>

        <el-form-item label="处理反馈" prop="handleText">
          <el-input v-model="form.handleText" type="textarea" placeholder="请输入内容" />
        </el-form-item>
       
        <el-form-item label="通知用户" prop="notifyUsers">
          <el-radio-group v-model="form.notifyUsers">
            <el-radio :label="0">全部</el-radio>
            <el-radio :label="1">举报人</el-radio>
            <el-radio :label="2">举报对象</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="通知方式">
          <el-radio-group v-model="form.notifyType">
            <el-radio :label="1">站内信</el-radio>
            <el-radio :label="2">短信</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="处理结果">
          <el-radio-group v-model="form.handleStatus">
            <el-radio :label="1">有效举报</el-radio>
            <el-radio :label="2">恶意举报</el-radio>
            <el-radio :label="3">举报对象</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUserReport, getUserReport, delUserReport, addUserReport, updateUserReport, exportUserReport } from "@/api/system/userReport";

export default {
  name: "UserReport",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 用户举报处理表格数据
      userReportList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: undefined,
        reportType: undefined,
        accountId: undefined,
        reportTime: undefined,
        reportAccountId: undefined,
        reportText: undefined,
        reportImgs: undefined,
        reportReason: undefined,
        reportSource: undefined,
        status: undefined,
        handleText: undefined,
        handleStatus: undefined,
        handleBy: undefined,
        handleTime: undefined,
        notifyType: undefined,
        notifyUsers: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        accountId: [
          { required: true, message: "举报人不能为空", trigger: "blur" }
        ],
        reportTime: [
          { required: true, message: "举报时间不能为空", trigger: "blur" }
        ],
        reportAccountId: [
          { required: true, message: "举报对象不能为空", trigger: "blur" }
        ],
        reportText: [
          { required: true, message: "举报内容不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "举报状态 0未处理、1已处理不能为空", trigger: "blur" }
        ],
        notifyType: [
          { required: true, message: "通知方式 1站内信2短信不能为空", trigger: "blur" }
        ],
        notifyUsers: [
          { required: true, message: "通知用户 0全部, 1举报人、2举报对象，不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户举报处理列表 */
    getList() {
      this.loading = true;
      listUserReport(this.queryParams).then(response => {
        this.userReportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        code: undefined,
        reportType: undefined,
        accountId: undefined,
        reportTime: undefined,
        reportAccountId: undefined,
        reportText: undefined,
        reportImgs: undefined,
        reportReason: undefined,
        reportSource: undefined,
        status: "0",
        handleText: undefined,
        handleStatus: "0",
        handleBy: undefined,
        handleTime: undefined,
        notifyType: undefined,
        notifyUsers: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户举报处理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUserReport(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户举报处理";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateUserReport(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addUserReport(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除用户举报处理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delUserReport(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户举报处理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportUserReport(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>